package cpa.prowon.gui;

import java.util.Properties;
import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import cpa.prowon.Application;
import cpa.prowon.gui.util.ImageUtil;

/**
 * The help window shows shows the about information. It gets opened
 * when clicking on the Help->About menu item and shows information about
 * the application
 * @author Chris
 */
public class HelpWindow {

	// Properties
	protected MainWindow window;
	protected ResourceBundle rb;
	protected Shell shell;
	
	/**
	 * Constructor for the HelpWindow
	 * @param MainWindow The main window which is the parent of the help window
	 */
	public HelpWindow(MainWindow mainWindow) {
		
		// Set the main window
		window = mainWindow;
		
		// Set the resource bundle
		rb = Application.getInstance().getResourceBundle();
		
	}
	
	/**
	 * Open the window
	 * @author Chris
	 */
	public void open() {
	
		// Create the and configure the shell
		shell = createShell();

		// Create the contents
		createContents(shell);
				
		// Open the shell
		shell.pack();
		shell.open ();
	
		// Dispose the shell
		Display display = shell.getDisplay();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch())
				display.sleep();
		}
	
	}

	/**
	 * Create the contents for the window
	 * @param Shell The parent where the contents will get put into
	 * @author Chris
	 */
	protected void createContents(Shell parent) {

		// Build the about information
		Properties intProps = Application.getInstance().getInternalProperties();
		String about = intProps.getProperty(Application.PROPNAME_APPLICATION_NAME) + "\n";
		about += "Version: " + intProps.getProperty(Application.PROPNAME_APPLICATION_VERSION) + "\n\n";
		about += "(c) 2003 by Peter Horvath, Christian Patterer\n";
		
		// Create the about information
		Label label = new Label(parent, 1);
		label.setText(about);		

		// Create the OK button
		Button okButton = new Button(shell, SWT.PUSH);
		GridData okButtonGridData = new GridData();
		okButtonGridData.horizontalAlignment = GridData.CENTER;
		okButtonGridData.widthHint = 100;
		okButton.setLayoutData(okButtonGridData);
		okButton.setText(Application.getInstance().getResourceBundle().getString("button_ok"));
		okButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				close();
			}
		});

		// Make it the default button
		parent.setDefaultButton(okButton);
		
	}
		
	/**
	 * Close the window
	 * @author Chris
	 */
	protected void close() {
		shell.dispose();
	}
	
	/**
	 * Create the shell that holds all the window widgest
	 * @return The actual shell holding all widgets
	 * @author Chris
	 */
	protected Shell createShell() {
	
		// Create the shell
		Shell shell = new Shell(window.getShell(), SWT.CLOSE | SWT.TITLE | SWT.APPLICATION_MODAL);
		GridLayout shellGrid = new GridLayout();
		shellGrid.marginWidth = 10;
		shellGrid.marginHeight = 5;
		shellGrid.makeColumnsEqualWidth = true;
		shell.setLayout(shellGrid);
		shell.setText (Application.getInstance().getResourceBundle().getString("title_helpabout"));
		shell.setImage(ImageUtil.getImage("icons/logo.gif"));
	
		// Return the shell
		return shell;
		
	}

}
